<?php

namespace app\api\controller;

use app\common\lib\Arr;
use app\common\lib\Show;
use think\response\Json;

class Category extends ApiBase
{

    public function getCategory(){
        $name = $this->request->param("name", "", "trim");
        try {
            $data = (new \app\common\business\Category())->getCategoryByName($name);
        } catch (\Exception $e) {
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }

    public function getCategoryMultiple()
    {
        if (!$this->request->isPost()) {
            return Show::error([],"非法请求");
        }

        $name = $this->request->param("name", "", "trim");

        try {
            $data=(new \app\common\business\Category())->getCategoryMultipleByName($name);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }
        return Show::success($data);
    }

    public function getCategoryMultipleForApplyRepair()
    {
        try {
            $data=(new \app\common\business\Category())->getCategoryByPid(config('apply_repair')['category_bxsq']);
        }catch (\Exception $e){
            return Show::error([], $e->getMessage(), $e->getCode());
        }

        if (!empty($data)){
            foreach ($data as $k=>$v){
                if ($v['pid'] == config('apply_repair')['category_bxsq']) $data[$k]['pid'] = 0;//方便前端计算主分类
            }
        }
        $result = Arr::getTree($data);
        return Show::success($result);
    }
}